package com.dhcc.DRGsHosp.application.impl;

import com.dhcc.DRGsHosp.application.MrIpInfoApplication;
import com.dhcc.DRGsHosp.core.domain.MrIpInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Named;
import java.util.List;
import java.util.Map;

/**
 * 首页表实体类 的   数据访问层--实体类
 * @ClassName: MrIpInfoApplicationImpl  
 * @Description ：首页表实体类  数据访问层实体类
 * @Date: 2018年7月26日 下午9:17:24  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */

@Named("drgsHospMrIpInfoApplicationImpl20181212")
@Transactional
public class MrIpInfoApplicationImpl implements MrIpInfoApplication {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;

	/**  
	 *  功能描述：取得一个首页表实体类的信息
	 *  @Author QiaoYiLong
	 *  @param pmainIndex  主索引
	 *  @Date:2018年7月26日下午9:10:59
	 */
	@Override
	public MrIpInfo getMrIpInfo(String pmainIndex) {		
		return MrIpInfo.get(MrIpInfo.class , pmainIndex);
	}

	/**
	 * @param condition
	 * @return
	 * @throws
	 * @Description: 查询病人信息
	 * @author chenhanlin
	 * @date 2018/7/29 15:51
	 */
	@Override
	public List<MrIpInfo> queryMrIpInfoByCondition(Map<String, Object> condition) {
		StringBuffer sqlb = new StringBuffer() ;
		sqlb.append(" SELECT\n") ;
		sqlb.append(" a.PMainIndex \"pmainIndex\",P00230000 \"p00230000\",P00230001 \"p00230001\",P00240000 \"p00240000\"," ) ;
		sqlb.append(" P00240001 \"p00240001\",P00250000 \"p00250000\",P00250100 \"p00250100\",P00260000 \"p00260000\"," ) ;
		sqlb.append(" P00260001 \"p00260001\",P00270000 \"p00270000\",P00270001 \"p00270001\",P00280000 \"p00280000\"," ) ;
		sqlb.append(" P00280001 \"p00280001\",P00290000 \"p00290000\",P00290100 \"p00290100\",P00300000 \"p00300000\"," ) ;
		sqlb.append(" P00300001 \"p00300001\",P00310000 \"p00310000\",P00310001 \"p00310001\",P00320000 \"p00320000\"," ) ;
		sqlb.append(" P00330000 \"p00330000\",P00330001 \"p00330001\",P00340000 \"p00340000\",P00350000 \"p00350000\"," ) ;
		sqlb.append(" P00360000 \"p00360000\",P00370000 \"p00370000\",P00380000 \"p00380000\",P00380001 \"p00380001\"," ) ;
		sqlb.append(" P00390000 \"p00390000\",P00400100 \"p00400100\",P00400200 \"p00400200\",P00400300 \"p00400300\"," ) ;
		sqlb.append(" P00410000 \"p00410000\",P00410001 \"p00410001\",P00420000 \"p00420000\",P00420001 \"p00420001\"," ) ;
		sqlb.append(" P00430000 \"p00430000\",P00430001 \"p00430001\",P00440000 \"p00440000\",P00440001 \"p00440001\"," ) ;
		sqlb.append(" P00450000 \"p00450000\",P00450001 \"p00450001\",P00460000 \"p00460000\",P00460001 \"p00460001\"," ) ;
		sqlb.append(" P00470000 \"p00470000\",P00470001 \"p00470001\",P00480000 \"p00480000\",P00480001 \"p00480001\"," ) ;
		sqlb.append(" P00490000 \"p00490000\",P00500000 \"p00500000\",P00510000 \"p00510000\",P00520000 \"p00520000\"," ) ;
		sqlb.append(" P00530000 \"p00530000\",P00540000 \"p00540000\",P00550000 \"p00550000\",P00550101 \"p00550101\"," ) ;
		sqlb.append(" P00550102 \"p00550102\",P00550103 \"p00550103\",P00560000 \"p00560000\",P00570000 \"p00570000\"," ) ;
		sqlb.append(" P00570001 \"p00570001\",P00580000 \"p00580000\",P00580001 \"p00580001\",P00590000 \"p00590000\"," ) ;
		sqlb.append(" P00590100 \"p00590100\",P00590200 \"p00590200\",P00590300 \"p00590300\",P00590400 \"p00590400\"," ) ;
		sqlb.append(" P00590500 \"p00590500\",P00600100 \"p00600100\",P00600101 \"p00600101\",P00600200 \"p00600200\"," ) ;
		sqlb.append(" P00600201 \"p00600201\",P00600300 \"p00600300\",P00600301 \"p00600301\",P00601000 \"p00601000\"," ) ;
		sqlb.append(" P00601001 \"p00601001\",P00600400 \"p00600400\",P00600401 \"p00600401\",P00600500 \"p00600500\"," ) ;
		sqlb.append(" P00600600 \"p00600600\",P00600700 \"p00600700\",P00600800 \"p00600800\",P00600900 \"p00600900\"," ) ;
		sqlb.append(" P00610000 \"p00610000\",P00610001 \"p00610001\",P00610100 \"p00610100\",P00610200 \"p00610200\"," ) ;
		sqlb.append(" P00610300 \"p00610300\",P00620000 \"p00620000\",P00620001 \"p00620001\",P00620100 \"p00620100\"," ) ;
		sqlb.append(" P00620200 \"p00620200\",P00630000 \"p00630000\",P00630001 \"p00630001\",P00630100 \"p00630100\"," ) ;
		sqlb.append(" P00640001 \"p00640001\",P00640002 \"p00640002\",P00640003 \"p00640003\",P00650001 \"p00650001\"," ) ;
		sqlb.append(" P00650002 \"p00650002\",P00650003 \"p00650003\",P00700000 \"p00700000\",P00910000 \"p00910000\"," ) ;
		sqlb.append(" P00920000 \"p00920000\",P00800000 \"p00800000\",P00810000 \"p00810000\",P00900000 \"p00900000\"," ) ;
		sqlb.append(" P00900001 \"p00900001\",P00400101 \"p00400101\",P00400201 \"p00400201\",P00400301 \"p00400301\"," ) ;
		sqlb.append(" P00930000 \"p00930000\",P00930001 \"p00930001\",P00930002 \"p00930002\",P00400401 \"p00400401\"," ) ;
		sqlb.append(" P00950000 \"p00950000\",P00950001 \"p00950001\",P00950002 \"p00950002\",P00960000 \"p00960000\"," ) ;
		sqlb.append(" P00960001 \"p00960001\",P00940001 \"p00940001\",P00940002 \"p00940002\" \n") ;
		sqlb.append(" FROM \n") ;
		sqlb.append(" drgs.MR_BASEINFO a\n") ;
		sqlb.append(" LEFT JOIN drgs.MR_IPINFO ipinfo ON a.PMAININDEX = ipinfo.PMAININDEX\n") ;
		sqlb.append(" WHERE 1=1 \n ") ;
		String sql = sqlb.toString() ;
		// 时间条件
		if(condition.get("dateFrom")!=null && condition.get("dateTo") !=null){
			String dateField = "" ;
			// 时间字段
			if("outDate".equals(condition.get("dateType"))){ // 出院日期
				dateField = "ipinfo.P00290000" ;
			} else if("pushDate".equals(condition.get("dateType"))) { // 推送日期
				dateField = "a.push_date" ;
			}
			sql = sql.replace("1=1", " 1=1 and to_char("+dateField+",'yyyy-mm-dd')  BETWEEN :dateFrom AND :dateTo ") ;
		}

		if(condition.get("hospCode") != null){
			sql = sql.replace("1=1" ," 1=1 and instr(:hospCode,a.p00010001)>0 ") ;
		}

		// 分页sql
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM (";
		String pageSqlFoot = " ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize ";
		List<MrIpInfo> mrIpInfoList = jdbcTempSeverice.query(pageSqlHead+sql+pageSqlFoot,condition , new BeanPropertyRowMapper<MrIpInfo>(MrIpInfo.class));
		return mrIpInfoList;
	}

}
